package com.aixuexi.item.service.impl;

import com.aixuexi.item.entity.ItemCategory;
import com.aixuexi.item.dao.ItemCategoryDao;
import com.aixuexi.item.service.ItemCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (ItemCategory)表服务实现类
 *
 * @author makejava
 * @since 2022-02-14 23:28:48
 */
@Service("itemCategoryService")
public class ItemCategoryServiceImpl implements ItemCategoryService {
    @Resource
    private ItemCategoryDao itemCategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ItemCategory queryById(Integer id) {
        return this.itemCategoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param itemCategory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ItemCategory> queryByPage(ItemCategory itemCategory, PageRequest pageRequest) {
        long total = this.itemCategoryDao.count(itemCategory);
        return new PageImpl<>(this.itemCategoryDao.queryAllByLimit(itemCategory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param itemCategory 实例对象
     * @return 实例对象
     */
    @Override
    public ItemCategory insert(ItemCategory itemCategory) {
        this.itemCategoryDao.insert(itemCategory);
        return itemCategory;
    }

    /**
     * 修改数据
     *
     * @param itemCategory 实例对象
     * @return 实例对象
     */
    @Override
    public ItemCategory update(ItemCategory itemCategory) {
        this.itemCategoryDao.update(itemCategory);
        return this.queryById(itemCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.itemCategoryDao.deleteById(id) > 0;
    }
}
